import { del, insertOrUpdate, insure, oil, page } from '../services/car';
import React from 'react';
import { DatePicker, Input, message } from 'antd';

/**
 * 客户数据模型
 */

//表格列表信息
const columns = [
  {
    title: '项目区域',
    dataIndex: 'projects',
    width: '120px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '归属',
    dataIndex: 'affiliate',
    width: '80px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '车牌号',
    dataIndex: 'carNo',
    width: '200px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '当前公里数',
    dataIndex: 'kilometres',
    width: '120px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '车型',
    dataIndex: 'carType',
    width: '120px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '行驶证发证日期',
    dataIndex: 'drLicSendDate',
    width: '140px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '行驶证注册日期',
    dataIndex: 'drLicRegDate',
    width: '140px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '年检日期',
    dataIndex: 'annualDate',
    width: '140px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '强制报废日期',
    dataIndex: 'scrapDate',
    width: '140px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '交强险到时日期',
    dataIndex: 'insureTraffic',
    width: '140px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '商业险到期日期',
    dataIndex: 'insureThird',
    width: '140px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '被保险人',
    dataIndex: 'insureName',
    width: '140px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '登记证书',
    dataIndex: 'regCer',
    width: '200px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '运输证',
    dataIndex: 'tranCer',
    width: '200px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '备注',
    dataIndex: 'remark',
    width: '200px',
    editable: true,
    ellipsis: true,
  },
];

const columnsOil = [
  {
    title: '油卡编号',
    dataIndex: 'oilNo',
    width: '150px',
  },
  {
    title: '车牌号',
    dataIndex: 'carNo',
    width: '80px',
  },
  {
    title: '司机',
    dataIndex: 'driver',
    width: '80px',
  },
  {
    title: '上次公里数',
    dataIndex: 'kilometresStart',
    width: '80px',
  },
  {
    title: '本次公里数',
    dataIndex: 'kilometresEnd',
    width: '80px',
  },
  {
    title: '加油升数',
    dataIndex: 'oilCount',
    width: '60px',
  },
  {
    title: '油价',
    dataIndex: 'oilPrice',
    width: '60px',
  },
  {
    title: '金额',
    dataIndex: 'oilAmount',
    width: '60px',
  },
  {
    title: '实际金额',
    dataIndex: 'oilOffAmount',
    width: '60px',
  },
  {
    title: '百公里油耗',
    dataIndex: 'kwh',
    width: '60px',
  },
  {
    title: '加油时间',
    dataIndex: 'oilTime',
    width: '120px',
  },
  {
    title: '加油支付单号',
    dataIndex: 'oilPayNo',
    width: '120px',
  },
];

const columnsInsure = [
  {
    title: '车牌号',
    dataIndex: 'carNo',
    width: '80px',
  },
  {
    title: '保险公司名称',
    dataIndex: 'comp',
    width: '80px',
  },

  {
    title: '保险单号',
    dataIndex: 'insureNo',
    width: '80px',
  },
  {
    title: '保险生效时间',
    dataIndex: 'insureTimeStart',
    width: '80px',
  },
  {
    title: '保险中止时间',
    dataIndex: 'insureTimeEnd',
    width: '60px',
  },
  {
    title: '保险类型',
    dataIndex: 'insureType',
    width: '60px',
  },
  {
    title: '保额',
    dataIndex: 'insureAmount',
    width: '60px',
  },
  {
    title: '保费',
    dataIndex: 'insureFee',
    width: '60px',
  },
  {
    title: '被保险人',
    dataIndex: 'insureName',
    width: '60px',
  },
];

// 搜索框字段
const searchFields = [
  {
    cols: [
      {
        md: 4,
        label: '车牌号',
        name: 'carNo',
        component: <Input placeholder="请输入车牌号" />,
      },
      {
        md: 4,
        label: '年审日期',
        name: 'annualDateStr',
        component: <DatePicker format="YYYY-MM-DD" />,
      },
      {
        md: 4,
        label: '强制报废日期',
        name: 'scrapDateStr',
        component: <DatePicker format="YYYY-MM-DD" />,
      },
      {
        md: 4,
        label: '交强险到期日期',
        name: 'insureTrafficStr',
        component: <DatePicker format="YYYY-MM-DD" />,
      },
      /*{
        md: 4,
        label: '保险类型',
        name: 'insureType',
        component: <Input placeholder="请输入保险类型" />,
      },*/
      {
        md: 4,
        label: '商业险到期日期',
        name: 'insureThirdStr',
        component: <DatePicker format="YYYY-MM-DD" />,
      },
      /* {
        md: 4,
        label: '',
        name: 'flag',
        component: <Radio.Group>
                    <Radio.Button value="oil">油耗</Radio.Button>
                    <Radio.Button value="insure">保险</Radio.Button>
                  </Radio.Group>
      }*/
    ],
  },
];

// 校验规则
const requiredCols = {
  车牌号: [
    {
      required: 'true',
      message: `请输入车牌号!`,
    },
  ],
};

export default {
  namespace: 'car',

  state: {
    searchFields: searchFields, // 搜索框信息
    searchValues: {
      flag: {
        name: 'flag',
        value: 'oil',
      },
    }, // 搜索框里的值
    pageList: [], // 表格数据
    pagination: {
      pageSize: 10,
      current: 1,
      size: 'small',
      showSizeChanger: true,
      showQuickJumper: true,
    }, // 表格分布数据
    columns: columns, // 表格列信息
    columnsOil: columnsOil,
    columnsInsure: columnsInsure,
    editId: '', // 编辑行id
    selectionIds: [], // 表格选择ID
    selectedRows: [], // 表格选择行
    requiredCols: requiredCols, // 必填字段
  },

  effects: {
    // 获取分页数据
    *page({ payload }, { call, put }) {
      const res = yield call(page, payload);
      yield put({
        type: 'page_r',
        payload: res,
      });
    },
    *save({ payload }, { call, put }) {
      const res = yield call(insertOrUpdate, payload);
      yield put({
        type: 'save_r',
        payload: res,
      });
    },
    *del({ payload }, { call, put }) {
      const res = yield call(del, payload);
      yield put({
        type: 'del_r',
        payload: res,
      });
    },
    *oil({ payload }, { call, put }) {
      const res = yield call(oil, payload);
      yield put({
        type: 'save_r',
        payload: res,
      });
    },
    *insure({ payload }, { call, put }) {
      const res = yield call(insure, payload);
      yield put({
        type: 'save_r',
        payload: res,
      });
    },
  },

  reducers: {
    // 新增
    handleAdd(state) {
      const { pageList, pagination } = state;
      let addList = pageList.filter(page => {
        return page.id === 0;
      });
      if (addList.length) {
        message.warning('请提交后再添加！');
        return {
          ...state,
        };
      } else {
        pageList.unshift({ id: 0, customerName: '新增' });
        const newPagination = {
          pages: pagination.pages,
          pageSize: pagination.pageSize,
          total: pagination.total + 1,
          current: pagination.current,
          size: 'small',
          showSizeChanger: true,
          showQuickJumper: true,
        };
        return {
          ...state,
          pageList: pageList,
          pagination: newPagination,
        };
      }
    },
    // 绑定搜索框数据
    handleSearchChange(state, action) {
      const { searchValues } = state;
      const newSearchForm = {
        ...searchValues,
        ...action.payload,
      };
      return {
        ...state,
        searchValues: newSearchForm,
      };
    },
    // 处理编辑
    handleEdit(state, action) {
      return {
        ...state,
        editId: action.payload,
      };
    },

    // 改变分页插件
    handlePagination(state, action) {
      const { payload } = action;
      const { pagination } = state;
      let newPagination = {
        ...pagination,
        ...payload,
      };
      return {
        ...state,
        pagination: newPagination,
      };
    },
    // 表格选中Id
    handleSelection(state, action) {
      const { payload } = action;
      return {
        ...state,
        selectionIds: payload.selectedRowKeys,
        selectedRows: payload.selectedRows,
      };
    },
    // 分页数据请求回来后处理
    page_r(state, action) {
      const { payload } = action;
      if (payload && payload.code === 0) {
        const list = payload.result.records;
        const pagination = {
          pages: payload.result.pages,
          pageSize: payload.result.size,
          total: payload.result.total,
          current: payload.result.current,
          size: 'small',
          showSizeChanger: true,
          showQuickJumper: true,
        };
        return {
          ...state,
          pagination: pagination,
          pageList: list,
          editId: '',
          selectionIds: [],
          selectedRows: [],
        };
      } else {
        return {
          ...state,
        };
      }
    },
    // 处理保存
    save_r(state, action) {
      const { payload } = action;
      if (payload && payload.code === 0) {
        message.success('保存成功！');
      } else {
        message.error(`${payload.errorMsg}` ? `${payload.errorMsg}` : '保存失败！');
      }
      return {
        ...state,
      };
    },
    // 处理删除
    del_r(state, action) {
      const { payload } = action;
      if (payload && payload.code === 0) {
        message.success('删除成功！');
      } else {
        message.error(`${payload.errorMsg}` ? `${payload.errorMsg}` : '删除失败！');
      }
      return {
        ...state,
      };
    },
  },
};
